package com.kintreda.ieco_server.module.account.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.kintreda.ieco_server.bean.account.Menu;
import com.kintreda.ieco_server.core.web.BaseController;
import com.kintreda.ieco_server.core.web.R;
import com.kintreda.ieco_server.module.account.service.IMenuService;
import com.kintreda.ieco_server.module.account.service.IRoleService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 菜单信息管理控制层
 * @author HuangZhongYao
 *
 */
@Controller
@RequestMapping("/menu")
public class MenuController extends BaseController {

	@Autowired
	private IMenuService menuService;
	@Autowired
	private IRoleService roleService;

	/**********************************************************
	 * 进入菜单信息管理主页
	 * @return
	 */
	@RequestMapping(value = "/query",method=RequestMethod.GET)
	public String query() {
		return "account/menu/menu-list";
	}

	@RequestMapping(value = "/query",method=RequestMethod.POST)
	@ResponseBody
	public Object query(Menu menu) {
		List<Menu> menus = this.menuService.getAll();
		return menus;
	}
	
	
	/******************************************************
	 * 1.获取所有菜单信息列表
	 * 2.获取角色已经授权的菜单信息列表
	 * @param menu
	 * @return
	 */
	@RequestMapping(value = "/querynews",method=RequestMethod.POST)
	@ResponseBody
	public Object querynews(String roleId) {
		
		Map<String, Object> map = new HashMap<String, Object>();
		
		List<Menu> menus = this.menuService.getAll();
		map.put("menus", menus);
		map.put("roleMenus", this.roleService.get(roleId).getMenus());
		
		return map;
	}
	
	@RequestMapping(value = "/add/{pid}",method=RequestMethod.GET)
	@ResponseBody
	public ModelAndView add(@PathVariable("pid") String pid) {
		
		ModelAndView mv = new ModelAndView("account/menu/menu-add");
		
		Menu parent = menuService.get(pid);
		
		mv.addObject("parent",parent);
		
		return mv;
	}
	
	@RequestMapping(value = "/add",method=RequestMethod.POST)
	@ResponseBody
	public R add(Menu menu) {
		try {
			menuService.save(menu);
			return R.ok();
		} catch (Exception e) {
			e.printStackTrace();
			return R.error("添加菜单发生错误:" + e.getMessage());
		}
	}
	
	@RequestMapping(value = "/edit/{id}",method=RequestMethod.GET)
	public ModelAndView edit(@PathVariable("id") String id){
		
		ModelAndView mv = new ModelAndView("account/menu/menu-edit");
		
		Menu menu = menuService.get(id);
		mv.addObject("menu", menu);
		
		Menu parent = menuService.get(menu.getPid());
		mv.addObject("parent",parent);
		
		return mv;
	}
	
	@RequestMapping(value = "/edit",method=RequestMethod.POST)
	@ResponseBody
	public R edit(Menu menu){
		try {
			Menu menuDB = menuService.get(menu.getId());
			menuDB.setName(menu.getName());
			menuDB.setPid(menu.getPid());
			menuDB.setUrl(menu.getUrl());
			menuDB.setIdentification(menu.getIdentification());
			menuDB.setDes(menu.getDes());
			menuDB.setMenuButton(menu.getMenuButton());
			menuDB.setIcoUrl(menu.getIcoUrl());
			menuDB.setOrderNum(menu.getOrderNum());
			menuService.update(menuDB);
			return R.ok();
		} catch (Exception e) {
			e.printStackTrace();
			return R.error("修改菜单发生错误:" + e.getMessage());
		}
	}
	
	@RequestMapping(value = "/delete/{id}")
	@ResponseBody
	public R delete(@PathVariable("id") String id){
		try {
			Menu menuDB = menuService.get(id);
			menuService.delete(menuDB);
			return R.ok();
		} catch (Exception e) {
			e.printStackTrace();
			return R.error("修改菜单发生错误:" + e.getMessage());
		}
	}
	
}
